package org.example.Utils;
import org.example.POJO.online.User;

import java.io.*;
import java.util.*;

public class UserFileManager {

    private static final String FILE_PATH = "src/main/java/org/example/File/user.txt";

    // 读取所有用户
    public List<User> readUsers() {
        List<User> users = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(FILE_PATH))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (!line.trim().isEmpty()) { // 避免空行
                    User user = parseUserFromString(line);
                    if (user != null) {
                        users.add(user);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return users;
    }

    // 将用户字符串解析为User对象
    private User parseUserFromString(String line) {
        if (line.startsWith("User{") && line.endsWith("}")) {
            line = line.substring(5, line.length() - 1);
            String[] parts = line.split(", ");
            Map<String, String> fields = new HashMap<>();
            for (String part : parts) {
                String[] keyValue = part.split("=");
                if (keyValue.length == 2) {
                    fields.put(keyValue[0].trim(), keyValue[1].replace("'", "").trim());
                }
            }
            return new User(
                    fields.get("username"),
                    fields.get("avatar"),
                    fields.get("server"),
                    ""
            );
        }
        return null;
    }

    // 写入用户到文件
    public void writeUser(User user) {
        List<User> users = readUsers();
        if (!users.contains(user)) {
            users.add(user);
            writeUsersToFile(users);
            System.out.println("用户添加:" + user);
        } else {
            System.out.println("用户名已存在:" + user);
        }
    }

    // 删除用户
    public void deleteUser(String username) {
        List<User> users = readUsers();
        Iterator<User> iterator = users.iterator();
        while (iterator.hasNext()) {
            User user = iterator.next();
            if (user.getUsername().equals(username)) {
                iterator.remove();
                System.out.println("4004user文件操作——————————> 文件删除用户" + user);
                break;
            }
        }
        writeUsersToFile(users);
    }
    //根据用户名获取头像
    public String getAvatar(String username) {
        List<User> users = readUsers();
        for (User user : users) {
            if (user.getUsername().equals(username)) {
                return user.getAvatar();
            }
        }
        return null;
    }

    // 将用户列表写入文件
    private void writeUsersToFile(List<User> users) {
        try (PrintWriter pw = new PrintWriter(new FileWriter(FILE_PATH))) {
            for (User user : users) {
                pw.println(user.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
